// +----------------------------------------------------------------------
// | Project:   Commons  
// +----------------------------------------------------------------------
// | CreateTime: 15/11/3  下午6:41
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import butterknife.ButterKnife;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public abstract class CommActivity extends AppCompatActivity implements CommViewInferface{
    public Context context;
    public Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getArgs(getIntent().getExtras());
        setContentView(setView());
        ButterKnife.bind(this);
        activity = this;
        context = this;
        OttoManager.register(this);
        setting();
        initView();
        setListener();
    }


    /**
     * 获得传入的参数
     */
    public abstract void getArgs(Bundle bundle);

    /**
     * z
     * 设置布局文件
     */
    public abstract int setView();

    /**
     * 初始化布局文件中的控件
     */
    public abstract void initView();
    /**
     * 初始化设置其他，一般用在baseactivity
     */
    public abstract void setting();

    /**
     * 设置监听器
     */
    public abstract void setListener();


    @Override
    public void onBackPressed() {
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        System.gc();
        super.onDestroy();
        ButterKnife.unbind(this);
        OttoManager.unregister(this);
    }

    protected void startActivity(Class cls) {
        startActivity(cls, null, false);
    }

    protected void startActivity(Class cls, boolean isfinish) {
        startActivity(cls, null, isfinish);
    }

    protected void startActivity(Class cls, Bundle bundle) {
        startActivity(cls, bundle, false);
    }

    protected void startActivity(Class cls, Bundle bundle, boolean isfinish) {
        if (cls == null || activity == null || activity.isFinishing()) return;
        Intent intent = new Intent();
        if (bundle != null)
            intent.putExtras(bundle);
        intent.setClass(activity, cls);
        startActivity(intent);
        if (isfinish) finish();
    }

    protected void setStyle(boolean status, boolean navigation) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            if (status)
                window.setFlags(
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            if (navigation)
                window.setFlags(
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

}
